package zjc.bookkeeping.homepage.addpage;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

import zjc.bookkeeping.R;
import zjc.bookkeeping.adapter.AddPagerAdapter;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.minepage.aAssetaccount.AssetaccountdetailActivity;
import zjc.bookkeeping.server.StaticData;

public class AddActivity extends AppCompatActivity implements View.OnClickListener {
    private ViewPager2 viewPager2;
    private ImageView backIv;
    TextView accountName;
    private ArrayList<Fragment> fragments;
    private ArrayList<String> titles;
    private IncomeFragment incomeFragment;
    private OutcomeFragment outcomeFragment;
    private TransferFragment transferFragment;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
        setContentView(R.layout.activity_add);
        init();
        AddPagerAdapter addPagerAdapter = new AddPagerAdapter(this,fragments);
        viewPager2.setAdapter(addPagerAdapter);
        relation();
        monitor();
    }

    @Override
    protected void onDestroy() {
        StaticData.setMethodName(null);
        super.onDestroy();
    }

    private void monitor() {
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void relation() {
        new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(titles.get(position));
            }
        }).attach();
    }

    private void init() {
        viewPager2 = findViewById(R.id.vp2_add_view);
        tabLayout = findViewById(R.id.tl_add_titles);
        backIv = findViewById(R.id.add_iv_back);
        backIv.setOnClickListener(this);

        accountName = findViewById(R.id.tv_account);
        accountName.setText(DBManager.getAccountName(DBManager.selectAccountId()));

        fragments = new ArrayList<>();
        outcomeFragment = new OutcomeFragment();
        incomeFragment = new IncomeFragment();
        transferFragment = new TransferFragment();
        fragments.add(outcomeFragment);
        fragments.add(incomeFragment);
        fragments.add(transferFragment);

        titles = new ArrayList<>();
        titles.add("支出");
        titles.add("收入");
        titles.add("转账");
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.add_iv_back:
                finish();
                break;
        }
    }
}